package com.itstherules.jmsunit.mock.activemq;

import static com.itstherules.jmsunit.JmsUnit.sender;
import static com.itstherules.jmsunit.JmsUnit.mock;
import static com.itstherules.jmsunit.expectation.Expectations.headers;
import static com.itstherules.jmsunit.expectation.XmlBasedExpectations.xPathDoesntExist;

import org.junit.Before;
import org.junit.Test;

import com.itstherules.jmsunit.JmsInlineMock;
import com.itstherules.jmsunit.JmsSender;
import com.itstherules.jmsunit.managers.QueueManagersFactory.QueueManagerType;

public class JmsXPathDoesntExistExpectationTest {

	private static String XML = "<root>" + "<shoot>" + "<abba/>"
			+ "<acdc></acdc>" + "<eno>eno</eno>" + "</shoot>" + "<boot>"
			+ "<UNKLE>UNKLEMix</UNKLE>" + "<lfo>lowFrequencyOscillation</lfo>"
			+ "<soundgarden>superunknown</soundgarden>" + "</boot>" + "</root>";

	private JmsInlineMock stub;

	private JmsSender sender;

	@Before
	public void create() throws Exception {
		stub = mock("vm://localhost:61616", QueueManagerType.ActiveMQ);
		sender = sender("blue", "vm://localhost:61616",
				QueueManagerType.ActiveMQ);
	}

	@Test
	public void correctThatXPathDoesntExist() throws Exception {
		stub.expects().through("blue").with(
				xPathDoesntExist("/root/shoot/astute"));

		sender.send(XML, headers("Blue2"));
		stub.verifyAndClose();
	}

	@Test(expected = IllegalArgumentException.class)
	public void xPathNullInvalidXpath() throws Exception {
		stub.expects().through("blue").with(
				xPathDoesntExist("\\badgers//on*&the^storm\n\\d"));

		sender.send(XML, headers("Blue2"));
		stub.verifyAndClose();
	}

	@Test
	public void xPathNotFound() throws Exception {
		stub.expects().through("blue").with(xPathDoesntExist("/rot/sdf"));

		sender.send(XML, headers("Blue2"));
		stub.verifyAndClose();
	}

	@Test(expected = AssertionError.class)
	public void xPathNullExists() throws Exception {
		stub.expects().through("blue").with(
				xPathDoesntExist("/root/shoot/abba"));

		sender.send(XML, headers("Blue2"));
		stub.verifyAndClose();
	}

	@Test(expected = AssertionError.class)
	public void xPathNullEmptyString() throws Exception {
		stub.expects().through("blue").with(
				xPathDoesntExist("/root/shoot/acdc"));

		sender.send(XML, headers("Blue2"));
		stub.verifyAndClose();
	}

	@Test(expected = AssertionError.class)
	public void xPathNullValueExists() throws Exception {
		stub.expects().through("blue")
				.with(xPathDoesntExist("/root/shoot/eno"));

		sender.send(XML, headers("Blue2"));
		stub.verifyAndClose();
	}

	@Test(expected = IllegalArgumentException.class)
	public void xPathNullNullXPath() throws Exception {
		stub.expects().through("blue").with(xPathDoesntExist(null));

		sender.send(XML, headers("Blue2"));
		stub.verifyAndClose();
	}

}
